


//
//  UIView+Layer.m
//  DatangLife
//
//  Created by 娜 on 2018/4/20.
//  Copyright © 2018年 DatangLife. All rights reserved.
//

#import "UIView+Layer.h"

@implementation UIView (Layer)

- (void)layerProjectionWithShadowOpacity:(CGFloat)shadowOpacity withShadowColor:(UIColor *)color{
    self.layer.shadowColor = color.CGColor;//shadowColor阴影颜色
    self.layer.shadowOffset = CGSizeMake(0,0);//shadowOffset阴影偏移,x向右偏移2，y向下偏移6，默认(0, -3),这个跟shadowRadius配合使用
    self.layer.shadowOpacity = shadowOpacity;//阴影透明度，默认0
    self.layer.shadowRadius = 3;//阴影半径，默认3
}
- (void)layerBorderWithWidth:(CGFloat)width {
    self.layer.borderWidth = width;
    self.layer.borderColor = [UIColor blackColor].CGColor;
}
- (void)layerBorderWithWidth:(CGFloat)width borderColor:(UIColor *)borderColor {
    self.layer.borderWidth = width;
    self.layer.borderColor = borderColor.CGColor;
}
- (void)layerRoundedCornersWithRadius:(CGFloat)radius {
    self.layer.cornerRadius = radius;
    [self configRasterize];
}

- (void)setLayerCornerRadius:(CGFloat)layerCornerRadius {
    self.layer.cornerRadius = layerCornerRadius;
    [self configRasterize];
}

- (CGFloat)layerCornerRadius {
    return self.layer.cornerRadius;
}

- (void)configRasterize{
    self.layer.masksToBounds = YES;
    // 栅格化 - 提高性能
    // 设置栅格化后，图层会被渲染成图片，并且缓存，再次使用时，不会重新渲染
    self.layer.rasterizationScale = [UIScreen mainScreen].scale;
    self.layer.shouldRasterize = YES;
}
- (void)layerRoundedCornersWithRadius:(CGFloat)cornerRadius byRoundingCorners:(UIRectCorner)rectCorner{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:rectCorner cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
  
}
+ (CAGradientLayer *)setGradualChangingColor:(UIView *)view from:(NSString *)fromHexColorStr toColor:(NSString *)toHexColorStr startPoint:(CGPoint)startP endPoint:(CGPoint)endP{
    CAGradientLayer *gradientlayer = [CAGradientLayer layer];
    gradientlayer.frame = view.bounds;
    gradientlayer.colors = @[(__bridge id)[UIColor colorWithHexString:fromHexColorStr].CGColor,(__bridge id)[UIColor colorWithHexString:toHexColorStr].CGColor];
    gradientlayer.startPoint = startP;
    gradientlayer.endPoint = endP;
    gradientlayer.locations = @[@0,@1];
    return gradientlayer;
}

@end
